package com.smap.common;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.TouchCancelEvent;
import com.google.gwt.event.dom.client.TouchCancelHandler;
import com.google.gwt.event.dom.client.TouchEndEvent;
import com.google.gwt.event.dom.client.TouchEndHandler;
import com.google.gwt.event.dom.client.TouchEvent;
import com.google.gwt.event.dom.client.TouchMoveEvent;
import com.google.gwt.event.dom.client.TouchMoveHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;

public abstract class TouchAdapter implements TouchCancelHandler,
		TouchEndHandler, TouchMoveHandler, TouchStartHandler {

	List<HandlerRegistration> handlers = new ArrayList<HandlerRegistration>();

	public void install(Widget widget) {
		handlers.add(widget.addDomHandler(this, TouchCancelEvent.getType()));
		handlers.add(widget.addDomHandler(this, TouchEndEvent.getType()));
		handlers.add(widget.addDomHandler(this, TouchMoveEvent.getType()));
		handlers.add(widget.addDomHandler(this, TouchStartEvent.getType()));
	}

	public void remove() {
		for (HandlerRegistration link : handlers) {
			link.removeHandler();
		}
	}

	public abstract void onEvent(@SuppressWarnings("rawtypes") TouchEvent evt);

	public void onTouchStart(TouchStartEvent event) {
		onEvent(event);

	}

	public void onTouchMove(TouchMoveEvent event) {
		onEvent(event);

	}

	public void onTouchEnd(TouchEndEvent event) {
		onEvent(event);

	}

	public void onTouchCancel(TouchCancelEvent event) {
		onEvent(event);

	}
}